package com.tools.file;

import lombok.extern.java.Log;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

/**
 * @Author LGJ
 * @create 2023/1/13 16:59
 * @Description 文件内容处理
 */
@Log
public class FileContentDispose {

    /**
     * 读取文件内容重新生成新内容格式文件
     * @param name 文件地址例如：D:\11111.txt
     * @param targetName 目标文件地址例如：D:\444.txt
     * @throws IOException
     */
    public static void toArrayByFileReader1(String name,String targetName) throws IOException {
        // 使用ArrayList来存储每行读取到的字符串
        ArrayList<String> arrayList = new ArrayList<>();
        try {
            FileReader fr = new FileReader(name);
            BufferedReader bf = new BufferedReader(fr);
            String str;
            // 按行读取字符串
            int count = 1;
            while ((str = bf.readLine()) != null) {
                count++;
                arrayList.add(str);
            }
            bf.close();
            fr.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 对ArrayList中存储的字符串进行处理
        int length = arrayList.size();
        int[] array = new int[length];
        try (FileWriter fileWriter = new FileWriter("D:\\444.txt",false)) {
            for (int i = 0; i < length; i++) {
                String s = arrayList.get(i).trim();
                int index = arrayList.get(i).indexOf(",");
                fileWriter.append("'" + s + "',");
                fileWriter.append("\n");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}
